<template>
  <div class="app-container">
    <el-form ref="dataForm" :model="dataForm" :rules="dataFormRules" label-width="140px">
      <el-form-item
        v-for="formField in formFields"
        :key="formField.prop"
        :label="formField.label"
        :prop="formField.prop"
      >

        <!--单选框-->
        <el-radio-group v-if="formField.type===1" v-model="dataForm[formField.prop]">
          <el-radio v-for="(item, index) in formField.dataSource" :key="index" :label="item.value"> {{ item.text }}</el-radio>
        </el-radio-group>
        <!--头像-->
        <div v-else-if="formField.type===2">
          <Upload v-model="dataForm[formField.prop]" :path="'user'" :limit="1" :value="dataForm[formField.prop]" />
        </div>
        <!-- 默认显示普通文本框 -->
        <el-input
          v-else
          v-model="dataForm[formField.prop]"
          size="mini"
          :type="`${formField.type}`"
          :placeholder="`请输入${formField.label}`"
          auto-complete="off"
          style="width: 30%"
        />
      </el-form-item>
      <el-button :loading="loading" type="primary" style="margin-left:5%;" @click.native.prevent="handleSubmit">提交</el-button>
    </el-form>
  </div>
</template>
<script>
import { updatePassword } from '../../api/modules/cm-system'
export default {
  data() {
    const checkPassWord = (rule, value, callback) => {
      const { newPassword, confirmPassword } = this.dataForm
      if (!this.validSomePsd(newPassword, confirmPassword)) {
        callback(new Error('两次密码必须一致'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      dataForm: {
        password: '',
        newPassWord: '',
        passwordAgain: ''
      },
      formFields: [
        { prop: 'password', label: '旧密码', minWidth: 120, type: 'password' },
        { prop: 'newPassWord', label: '新密码', minWidth: 120, type: 'password' },
        { prop: 'passwordAgain', label: '确认密码', minWidth: 120, type: 'password' }
      ],
      dataFormRules: {
        password: [{ required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPassWord: [{ required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: checkPassWord, trigger: 'blur' }
        ],
        passwordAgain: [{ required: true, message: '确认新密码', trigger: 'blur' },
          { validator: checkPassWord, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    validSomePsd(againPwd, password) {
      if (againPwd !== '' && password !== '') {
        if (againPwd !== password) {
          return false
        } else {
          return true
        }
      } else {
        return true
      }
    },
    // 提交
    handleSubmit() {
      this.$refs['dataForm'].validate(async(valid) => {
        if (!valid) {
          return false
        } else {
          this.loading = true
          try {
            const res = await updatePassword(this.dataForm)
            if (res.code === 0) {
              this.loading = false
              this.$notify({ title: '成功', message: '保存成功，请重新登录', type: 'success', duration: 2000 })
              setTimeout(async() => {
                await this.$store.dispatch('user/logOut')
                this.$router.push('/login')
              }, 200)
            } else {
              this.loading = false
              this.$message({ message: res.msg, type: 'error' })
            }
          } catch (e) {
            this.loading = false
            this.$message({ message: '操作失败', type: 'error' })
          }
        }
      })
    }
  }
}
</script>
